package com.wl.user.login.controller;

import com.wl.bean.Result;
import com.wl.user.login.biz.VerificationCodeBiz;
import jakarta.mail.MessagingException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/lvuser")
public class AuthController {

    private final VerificationCodeBiz verificationCodeBiz;

    public AuthController(VerificationCodeBiz verificationCodeBiz) {
        this.verificationCodeBiz = verificationCodeBiz;
    }

    @PostMapping("/send-code")
    public Result sendVerificationCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");

        try {
            verificationCodeBiz.sendVerificationCode(email);
            return new Result(1,"验证码发送成功",null);
        } catch (MessagingException e) {
            return new Result(0,"验证码发送失败",null);
        }
    }

    @PostMapping("/verify-code")
    public Result verifyCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = request.get("code");

        if (verificationCodeBiz.verifyCode(email, code)) {
            return new Result(1,"验证码正确",null);
        } else {
            return new Result(0,"验证码已过期，请重新发送",null);
        }
    }
}
